<?
require_once('unicode.php');

$debug_msg = false;

function print_data($entries)
{
  printf("data: [null,");
  for($i=0;$i<count($entries);$i++) {
    $entry = $entries[$i];
    printf("0x%x,",$entry[1]);
  }
  printf("-1],\n");
}
function print_index($indexes)
{
  printf("index: {\n");
  
  foreach($indexes as $code => $index) {
    $line = sprintf("'%s': {",$code);
    foreach($index as $key => $next) {
      $line .= sprintf("'%s': %d,",$key,$next);
    }
    $line = rtrim($line,',');
    $line .= "},";
    printf("%s\n",$line);
  }
  printf("}\n");
}
function print_entry($key,$entry,$start,$end = false)
{
  if($start >= count($entry)) return;

  $line = sprintf("'%s': [",$key);
  for($i=$start;$i<count($entry);$i++) {
    $value = $entry[$i];
    if($value >= 0xd800 && $value <= 0xdfff) die("Error\n");
    if($value > 0x10ffff) die("Error\n");
    $line .= sprintf("0x%x,",$value);

    if($end !== false && $i >= $end) break;
  }
  $line = rtrim($line,',');
  $line .= "],";
  printf("%s\n",$line);
}
function set_table($key,$type,$val)
{
  global $index;

  if(key_exists($key,$index)) $tmp = $index[$key];
  else $tmp = array();

  $tmp[$type] = $val;
  $index[$key] = $tmp;
}
function set_table_if_not_exists($key,$type,$val)
{
  global $index;

  if(key_exists($key,$index)) $tmp = $index[$key];
  else $tmp = array();

  if(!key_exists($type,$tmp)) {  
    $tmp[$type] = $val;
    $index[$key] = $tmp;
  }
}
if(count($argv) < 2) return;

$stderr = fopen("php://stderr","w");

fprintf($stderr,"Reading the %s...",$argv[1]);

$fp = fopen($argv[1],'r');
$entries = array();
$bindex = array();
$i = 0;
$last_key = null;
$last_index = 0;
$count = 0;
while($line = fgets($fp)) {
  $line = rtrim($line,"\n");
  $pos = strpos($line,' ');
  $txt = substr($line,0,$pos);
  $data = substr($line,$pos+1);
  $scalar = readutf8($data);

  if($txt != $last_key) {
    if($last_key) $bindex[$last_key] = array($last_index,$count);
    $last_key = $txt;
    $last_index = $i+1;
    $count = 0;
  }
  $entries[$i++] = array($txt,$scalar);
  $count++;
}
fclose($fp);

print_data($entries);

fprintf($stderr,"%d\nBuilding index...",count($entries));

// build index..
$index = array();
$stack = array();
$last_key = null;
foreach($bindex as $key => $tmp) {
  $no = $tmp[0];
  set_table($key,'@',$no);
  set_table($key,'#',$tmp[1]);

  $debug_msg && printf("reading %s -> %d (last_key=%s)\n",$key,$no,$last_key);
  if(!$last_key) {
    $last_key = $key;
    continue;
  }
  
  if($last_key === $key) continue;
  else if(strlen($last_key) < strlen($key) && ($pos = strpos($key,$last_key)) === 0) {
    $remain = substr($key,strlen($last_key));
    for($i=0,$str1=$last_key,$str2=$remain[0];$i<strlen($remain);$str1.=$remain[$i++],$str2=$remain[$i]) {
      $debug_msg && printf("set_table(%s,%s,%d)\n",$str1,$str2,$no);
      set_table($str1,$str2,$no);
      set_table_if_not_exists($str1,'@',$no);
    }

    $debug_msg && printf("push (%s)\n",$last_key);
    array_push($stack,$last_key);
    $last_key = $key;
  }
  else {
    $last_key = array_pop($stack);
    $debug_msg && printf("pop (%s)\n",$last_key);
    
    while($last_key != null && ($pos = strpos($key,$last_key)) !== 0) {
      $last_key = array_pop($stack);
      $debug_msg && printf("pop (%s)\n",$last_key);
    }

    if($last_key == null) { $last_key = $key; continue; }
  }
}
//print_r($index);
print_index($index);

fprintf($stderr,"Done.\n");


?>
